/*
 * @Description:
 * @Author: chaizhou
 * @Date: 2023-02-16 17:16:58
 * @LastEditTime: 2023-03-03 09:33:58
 */
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import locale from 'element-plus/lib/locale/lang/zh-cn' // 中文
import './public-path.js'
import actions from './stores/actions'
import { useShareStore } from '@/stores/useShareStore'

import App from './App.vue'
import { router } from './router'

import './assets/main.css'

import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

let app = null
// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  app = createApp(App)
  // app.use(ElementPlus, {
  //   locale: locale,
  //   size: 'default'
  // })
  app.use(createPinia())
  app.mount('#lawsuit')
} else {
  // 作为微应用运行
  renderWithQiankun({
    // 调用renderWithQiankun
    mount(props) {
      console.log(props)
      console.log('lawsuit mount')
      app = createApp(App)
      app.use(createPinia())
      app.use(router(props._parent_base))
      // app.use(ElementPlus, {
      //   locale: locale,
      //   size: 'default'
      // })
      app.mount(props.container ? props.container.querySelector('#lawsuit') : '#lawsuit')
      // 挂载父应用actions
      if (props.actions) {
        // 此处必须用props.onGlobalStateChange来覆盖 不然会出现监听事件（onGlobalStateChange）被覆盖问题
        actions.setActions({
          ...props.actions,
          onGlobalStateChange: props.onGlobalStateChange,
          setGlobalState: props.setGlobalState
        })
        props.onGlobalStateChange((state, prev) => {
          // console.log('子组件中检测到state变更：', state, prev)
          const shareStore = useShareStore()
          shareStore.$patch(state)
        }, true)
      }
    },
    bootstrap() {
      // console.log('-- bootstrap --')
    },
    update() {
      // console.log('-- update --')
    },
    unmount() {
      // console.log('-- unmount --')
      app?.unmount()
    }
  })
}
